# proftpd-lib.pl
# Common functions for the proftpd server config file

do '../web-lib.pl';
&init_config();

#get_ftp_dir($dirname)
#search in /etc/nas/shareftp config file
#return $dir_trust_uglist such as usertype,name,accemode,ul_bw,uu,dl_bw,du,maxlink:usertype,name,accemode,ul_bw,uu,dl_bw,du,maxlink:...
sub get_ftp_dir()
{
	my ($dirname, $dircfg, $find, $i, @line, @ftpfile, $dir_trust_uglist, $k);
	my (%ulist, %glist, $uu, $du, $maxlink, @uname, @gname);

	$dirname = @_[0];
	$find = 0;
	$dir_trust_uglist = "";

	#=======read shareftp===========
	open(SHAREFTP, $config{'shareftp'});
	@ftpfile = <SHAREFTP>;
	close(SHAREFTP);

	#=======search dir configration in shareftp============
	for($i = 0 ; $i < @ftpfile; $i++)
	{
		$ftpfile[$i] =~ s/#.*$//;		
		chomp($ftpfile[$i]);
		
		if($ftpfile[$i] eq "<$dirname>")
		{
			$find = 1;
			next;
		}

		if($find == 1)
		{
			if($ftpfile[$i] eq '<end>')
			{
				last;
			}
			if($ftpfile[$i] =~ /^rouser/)
			{
				$ftpfile[$i] =~ s/^rouser\s*=\s*//;
			#	$ftpfile[$i] =~ s/^\s+//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					if($k =~ /\\/)
					{
						$ulist{$k} = "2,".$k.",0";
						push @uname, $k;      #uname
					}
					else
					{
						$ulist{$k} = "0,".$k.",0";
						push @uname, $k;
					}
				}
			}
			if($ftpfile[$i] =~ /^rogroup/)
			{
				$ftpfile[$i] =~ s/^rogroup\s*=\s*//;
			#	$ftpfile[$i] =~ s/^\s+//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					if($k =~ /\\/)
					{
						$glist{$k} = "3,".$k.",0";
						push @gname, $k;    #gname
					}
					else
					{
						$glist{$k} = "1,".$k.",0";
						push @gname, $k;
					}
				}
			}
			if($ftpfile[$i] =~ /^rwuser/)
			{
				$ftpfile[$i] =~ s/^rwuser\s*=\s*//;
			#	$ftpfile[$i] =~ s/^\s+//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					if($k =~ /\\/)
					{
						$ulist{$k} = "2,".$k.",1";
						push @uname, $k;
					}
					else
					{
						$ulist{$k} = "0,".$k.",1";
						push @uname, $k;
					}
				}
			}
			if($ftpfile[$i] =~ /^rwgroup/)
			{
				$ftpfile[$i] =~ s/^rwgroup\s*=\s*//;
			#	$ftpfile[$i] =~ s/^\s+//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					if($k =~ /\\/)
					{
						$glist{$k} = "3,".$k.",1";
						push @gname, $k;
					}
					else
					{
						$glist{$k} = "1,".$k.",1";
						push @gname, $k;
					}
				}
			}
		}
	}

#===========get each user config===========
	open(FTPPASSWD, $config{'storftppasswd'});
	@ftpfile = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = 0; $i < @ftpfile; $i++)
	{
		$ftpfile[$i] =~ s/#.*$//;		
		chomp($ftpfile[$i]);

		@line = split(":", $ftpfile[$i]);
		if($ulist{$line[0]})
		{
			$uu = 0; $du = 0;
			if($line[6])
			{
				 $line[6] = $line[6]/ 1024;
			}
			else
			{
				$line[6] = 0;
			}
			if($line[7])
			{
				 $line[7] = $line[7]/ 1024;
			}
			else
			{
				$line[7] = 0;
			}
			if($line[6] >= 1024)
			{
				$uu = 1;
				$line[6] = $line[6] / 1024;
			}
			if($line[7] >= 1024)
			{
				$du = 1;
				$line[7] = $line[7]/ 1024;
			}
			unless($line[10])
			{
				$line[10] = 0;
			}
			$ulist{$line[0]} = $ulist{$line[0]}.",$line[6],$uu,$line[7],$du,$line[10]";
		}
	}

#===========get each group config===========
	open(FTPPASSWD, $config{'storftpgroup'});
	@ftpfile = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = 0; $i < @ftpfile; $i++)
	{
		$ftpfile[$i] =~ s/#.*$//;		
		chomp($ftpfile[$i]);

		@line = split(":", $ftpfile[$i]);
		if($glist{$line[0]})
		{
			$uu = 0; $du = 0;
			if($line[3])
			{
				 $line[3] = $line[3]/ 1024;
			}
			else
			{
				$line[3] = 0;
			}
			if($line[4])
			{
				 $line[4] = $line[4]/ 1024;
			}
			else
			{
				$line[4] = 0;
			}
			if($line[3] >= 1024)
			{
				$uu = 1;
				$line[3] = $line[3]/ 1024;
			}
			if($line[4] >= 1024)
			{
				$du = 1;
				$line[4] = $line[4]/ 1024;
			}
			unless($line[7])
			{
				$line[7] = 0;
			}
			$glist{$line[0]} = $glist{$line[0]}.",$line[3],$uu,$line[4],$du,$line[7]";
		}
	}

#============add to $dir_trust_uglist===================

	for($i = 0; $i < @uname; $i++)
	{
		if($dir_trust_uglist)
		{
			$dir_trust_uglist = $dir_trust_uglist . ":" . $ulist{$uname[$i]};
		}
		else
		{
			$dir_trust_uglist = $ulist{$uname[$i]};
		}
	}

	for($i = 0; $i < @gname; $i++)
	{
		if($dir_trust_uglist)
		{
			$dir_trust_uglist = $dir_trust_uglist . ":" . $glist{$gname[$i]};
		}
		else
		{
			$dir_trust_uglist = $glist{$gname[$i]};
		}
	}
	return $dir_trust_uglist;
}


#share_ftp_reload(\%sd, \%pw, \%gp)
sub share_ftp_reload()
{
	my (%glist, %ulist, %gcfg, @ftpfile, $dirname, @dirlist, $sd, $pw, $gp);
	my (@line, $i, $k, $idx);

	$sd = @_[0];
	$pw = @_[1];
	$gp = @_[2];

	$out = &ez_system("cp", "-f", $config{'storftppasswd'}, $config{'ftppasswd'});
	$out = &ez_system("cp", "-f", $config{'storftpgroup'}, $config{'ftpgroup'});
	
	open(FTPSHARE, $config{'shareftp'});
	@ftpfile = <FTPSHARE>;
	close(FTPSHARE);

	#=======search dir configration in shareftp============
	$idx = 0;
	for($i = 0 ; $i < @ftpfile; $i++)
	{
		$ftpfile[$i] =~ s/#.*$//;		
		chomp($ftpfile[$i]);
		
		if($ftpfile[$i] =~ /^<\/(.*)>$/)
		{
			$dirname = "/".$1."/./";
			push @dirlist, $dirname;
			next;
		}

		if($ftpfile[$i] eq "<end>")
		{
			$dirname = "";
			next;
		}

		if($dirname)
		{
#			print "<p>              ", $dirname;			
			if($ftpfile[$i] =~ /^rouser/)
			{
				$ftpfile[$i] =~ s/^rouser\s*=\s*//;
			#	$ftpfile[$i] =~ s/^\s+//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					$ulist{$k} = [($dirname, "0")] unless($ulist{$k});

				}
			}
			if($ftpfile[$i] =~ /^rogroup/)
			{
				$ftpfile[$i] =~ s/^rogroup\s*=\s*//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					$glist{$k} = [($dirname, "0", $idx++)] unless($glist{$k});
				}
			}
			if($ftpfile[$i] =~ /^rwuser/)
			{
				$ftpfile[$i] =~ s/^rwuser\s*=\s*//;
			#	$ftpfile[$i] =~ s/^\s+//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					$ulist{$k} = [($dirname, "1")] unless($ulist{$k});
				}
			}
			if($ftpfile[$i] =~ /^rwgroup/)
			{
				$ftpfile[$i] =~ s/^rwgroup\s*=\s*//;
			#	$ftpfile[$i] =~ s/^\s+//;
				@line = split(/\s*,\s*/, $ftpfile[$i]);
				foreach $k (@line)
				{
					$glist{$k} = [($dirname, "1", $idx++)] unless($glist{$k});
				}
			}
		}
	}

#===========refresh ftppasswd file===============
	open(FTPPASSWD, $config{'ftppasswd'});
	@ftpfile = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = 0; $i < @ftpfile; $i++)
	{
		$ftpfile[$i] =~ s/#.*$//;
		chomp($ftpfile[$i]);
		
		@line = split(":", $ftpfile[$i]);
		if($ulist{$line[0]})
		{
			$line[5] = $ulist{$line[0]}->[0];     #dir
			$line[18] = $ulist{$line[0]}->[1];    #rw option
		}
		else
		{
			$line[18] = "-1";    #rw option
		}
		$ftpfile[$i] = join(":", @line);
		$ftpfile[$i] = $ftpfile[$i]."\n";
	}

#	for($i = 0; $i < @ftpfile; $i++)
#	{
#		print "<p>", $ftpfile[$i];
#	}

	open(FTPPASSWD, ">$config{'ftppasswd'}");
	print FTPPASSWD @ftpfile;
	close(FTPPASSWD);

#===========refresh ftpgroup file===============

	open(FTPGROUP, $config{'ftpgroup'});
	@ftpfile = <FTPGROUP>;
	close(FTPGROUP);

	for($i = @ftpfile - 1; $i >= 0; $i--)
	{
		$ftpfile[$i] =~ s/#.*$//;
		chomp($ftpfile[$i]);
		
		@line = split(":", $ftpfile[$i]);
		if($glist{$line[0]})
		{
			$line[2] = $glist{$line[0]}->[0];     #dir
			$line[15] = $glist{$line[0]}->[1];    #rw option
			$gcfg{$line[0]} = join(":", @line);
			$gcfg{$line[0]} = $gcfg{$line[0]} . "\n";
		}
	}

	@ftpfile = ();
	
	foreach $k (keys %glist)
	{
		if($gcfg{$k})
		{			
			push @ftpfile, $gcfg{$k} ;
		}
		else
		{
			@line = ();
			$line[0] = $k;
			if($gp->{$k}->[0])
			{
				$line[1] = $gp->{$k}->[2];
			}
			else
			{
				next;
			}
			$line[2] = $glist{$k}->[0];    #dir
			$line[15] = $glist{$k}->[1];   #rw option
			push @ftpfile, join(":", @line)."\n" ;
		}
	}

	@ftpfile = sort {$glist{&fst_element_by_linestr($a, ":")}->[2] <=> $glist{&fst_element_by_linestr($b, ":")}->[2]} @ftpfile;

	open(FTPGROUP, ">$config{'ftpgroup'}");
	print FTPGROUP  @ftpfile;
	close(FTPGROUP);	

#	for($i = 0; $i < @ftpfile; $i++)
#	{
#		print "<p>", $ftpfile[$i];
#	}
}

#return linstr first element, for split with split_var
sub fst_element_by_linestr()
{
	my ($linstr, $split_var ,@line);

	$linstr = @_[0];
	$split_var = @_[1];

	@line = split (":", $linstr);

	return $line[0];
}

#sync ftppasswd with getent passwd
#sync_ftppasswd_with_getent(\%sd, \%pw)
sub sync_ftppasswd_with_getent()
{
	my ($i, @line, %ftpusr, @ftpfile, $k, @getpasswd);
	my ($str, @shadow, $sd, $pw, $uname);
	
	$sd = @_[0];
	$pw = @_[1];

	open(FTPPASSWD, $config{'ftppasswd'});
	@ftpfile = <FTPPASSWD>;	
	close(FTPPASSWD);

#	open(SHADOW, $config{'shadow'});
#	@shadow = <SHADOW>;
#	close(SHADOW);

#	for($i = 0; $i < @shadow; $i++)
#	{
#		$shadow[$i] =~ s/#.*$//;		
#		@line = split(":", $shadow[$i]);
#		if($line[0])
#		{
#			$sd{$line[0]} = $line[1];
#		}
#	}

	for($i = 0; $i < @ftpfile; $i++)
	{
		$ftpfile[$i] =~ s/#.*$//;
		chomp($ftpfile[$i]);
		if($ftpfile[$i])
		{
			@line = split(":", $ftpfile[$i]);
			$ftpusr{$line[0]} = $ftpfile[$i];
		}
	}

	@ftpfile = ();

	&foreign_require("setpass", "setpass-lib.pl");

#	@getpasswd = `getent passwd`;

	foreach $k (keys %$pw)
	{
		$uname = $pw->{$k}->[0];
		if($ftpusr{$uname})          #uname $pw->{$k}->[0]
		{
			@line = split(":", $ftpusr{$pw->{$k}->[0]});
			if($sd->{$uname}->[0])
			{
				$line[1] = $sd->{$uname}->[1];   #shadow passwd
			}
			else
			{
				$line[1] = "!!";              #for domainuser
			}
			$line[2] = $pw->{$k}->[2];        #uid
			$line[3] = $pw->{$k}->[3];        #gid
			$line[4] = $pw->{$k}->[4];        #fullname

			push @ftpfile, join(":", @line) . "\n";

#			push @ftpfile, $ftpusr{$pw->{$k}->[0]};  #config line $ftpusr{uname}
		}
		else
		{
			@line = ();
			$line[0] = $uname;
			if($sd->{$uname}->[0])
			{
				$line[1] = $sd->{$uname}->[1];   #shadow passwd
			}
			else
			{
				$line[1] = "!!";              #for domainuser
			}
			$line[2] = $pw->{$k}->[2];        #uid
			$line[3] = $pw->{$k}->[3];        #gid
			$line[4] = $pw->{$k}->[4];        #fullname
			$line[5] = $pw->{$k}->[5];        #dir
			$line[6] = "";                    #upload bandwidth
			$line[7] = "";                    #download bandwidth
			$line[18] = "-1";                 #rw
			$str = join(":", @line);
			$str = $str."\n";
			push @ftpfile, $str;
		}
	}

	open(FTPPASSWD, ">$config{'ftppasswd'}");
	print FTPPASSWD @ftpfile;	
	close(FTPPASSWD);

#	for($i = 0; $i < @ftpfile; $i++)
#	{
#		print "<p>", $ftpfile[$i];
#	}
}

#sync ftpgroup with getent group
#delete item not in getent group; ***********not same as sync_ftppasswd***********
#sync_ftpgroup_with_getent(\%gp)
sub sync_ftpgroup_with_getent()
{
	my ($i, @line, $gp, @ftpfile, $k, @getgroup);
	my ($str);

	$gp = @_[0];

#	@getgroup = `getent group`;

#	for($i = 0; $i < @getgroup; $i++)
#	{
#		$getgroup[$i] =~ s/#.*$//;
#		@line = split(":", $getgroup[$i]);
#		if($line[0])
#		{
#			$gp{$line[0]} = "yes";
#		}
#	}

	open(FTPGROUP, $config{'ftpgroup'});
	@ftpfile = <FTPGROUP>;
	close(FTPGROUP);

	for($i = @ftpfile -1 ; $i >= 0; $i--)
	{
		$ftpfile[$i] =~ s/#.*$//;
		chomp($ftpfile[$i]);
		@line = split(":", $ftpfile[$i]);
		if($line[0])
		{
			if($gp->{$line[0]}->[0])
			{
				$line[1] = $gp->{$line[0]}->[2];    #gid
				$ftpfile[$i] = join(":", @line);
			}
			else
			{
				splice(@ftpfile, $i, 1);
				next;
			}
		}
		$ftpfile[$i] = $ftpfile[$i] . "\n";
	}

#	for($i = 0; $i < @ftpfile; $i++)
#	{
#		print "<p>", $ftpfile[$i];
#	}

	open(FTPGROUP, ">$config{'ftpgroup'}");
	print FTPGROUP @ftpfile;
	close(FTPGROUP);
}


#set_cfg_for_dir(\@trustulist, $dirname, \%sd, \%pw, \%gp)
#each element is {"name"=>name, "dir"=>dir, "ulbw"=>ulbw, "dlbw"=>dlbw, "rw"=>rw, "maxlink"=>maxlink, "type"=>"group"/"user"}
sub set_cfg_for_dir()
{
	my ($ptulist, $pw, $sd, $gp, $dirname, $find, $str, %glist, %ulist, $i, $k, $k1, $k2, @line, @ftpshare, @dirstrct);
	my (@rwuser, @rouser, @rwgroup, @rwgroup, $out);
	
	$ptulist = @_[0];
	$dirname = @_[1];
	$sd = @_[2];
	$pw = @_[3];
	$gp = @_[4];

	for($i = 0; $i < @$ptulist; $i++)
	{
		if($ptulist->[$i]->{"rw"} > 0)
		{
			if($ptulist->[$i]->{"type"} eq "user")
			{
				push @rwuser, $ptulist->[$i]->{"name"};
				$ulist{$ptulist->[$i]->{"name"}} = $ptulist->[$i];

			}
			if($ptulist->[$i]->{"type"} eq "group")
			{
				push @rwgroup, $ptulist->[$i]->{"name"};
				$glist{$ptulist->[$i]->{"name"}} = $ptulist->[$i];
			}
		}
		if($ptulist->[$i]->{"rw"} == 0)
		{
			if($ptulist->[$i]->{"type"} eq "user")
			{
				push @rouser, $ptulist->[$i]->{"name"};
				$ulist{$ptulist->[$i]->{"name"}} = $ptulist->[$i];
			}
			if($ptulist->[$i]->{"type"} eq "group")
			{
				push @rogroup, $ptulist->[$i]->{"name"};
				$glist{$ptulist->[$i]->{"name"}} = $ptulist->[$i];
			}
		}
	}

#==========delete shareftp dir===============

	open(SHAREFTP, $config{'shareftp'});
	@shareftp = <SHAREFTP>;
	close(SHAREFTP);

	$find = -1;
	for($i = 0; $i < @shareftp; $i++)
	{
		$shareftp[$i] =~ s/#.*$//;
		chomp($shareftp[$i]);
		
		if($shareftp[$i] eq "<$dirname>")
		{
			$find = $i;
			next;
		}
		if($find >= 0)
		{
			if($shareftp[$i] eq "<end>")
			{
				splice @shareftp, $find, $i - $find + 1;
				last;
			}			
		}
		$shareftp[$i] = $shareftp[$i]."\n";
	}

	push @dirstrct, "<$dirname>\n";
	$str = join(",", @rwuser);
	push @dirstrct, "rwuser = ".$str . "\n";
	$str = join(",", @rouser);
	push @dirstrct, "rouser = ".$str . "\n";
	$str = join(",", @rwgroup);
	push @dirstrct, "rwgroup = ".$str . "\n";
	$str = join(",", @rogroup);
	push @dirstrct, "rogroup = ".$str . "\n";
	push @dirstrct, "<end>\n";

	splice @shareftp, 0, 0, @dirstrct;

	open(SHAREFTP, ">$config{'shareftp'}");
	print SHAREFTP @shareftp;
	close(SHAREFTP);

	foreach $k (@$ptulist)
	{
		$ulist{$k->{"name"}}=$k if($k->{type} eq "user");
		$glist{$k->{"name"}}=$k if($k->{type} eq "group");
	}

	&stor_ftppasswd_with_dir(\%ulist, $sd, $pw, $dirname);
	&stor_ftpgroup_with_dir(\%glist, \%gp, $dirname);

#	&set_ftppasswd(\%ulist, $sd, $pw);
#	&set_ftpgroup(\%glist, $gp);
}

#set_ftppasswd(\%ulist, \%sd, \%pw)
sub set_ftppasswd()
{
	my ($k, $ptulist, @ftppasswd, $i, @line, $sd, $pw);
	$ptulist = @_[0];
	$sd = @_[1];
	$pw = @_[2];

	open(FTPPASSWD, $config{'ftppasswd'});
	@ftppasswd = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = @ftppasswd - 1; $i >= 0; $i--)
	{
		$ftppasswd[$i] =~ s/#.*$//;
		@line = split(":", $ftppasswd[$i]);
		if($line[0])
		{
			if($ptulist->{$line[0]}->{"name"} && ($ptulist->{$line[0]}->{"type"} eq "user"))
			{
				splice @ftppasswd, $i, 1;
			}
		}
	}

	&foreign_require("setpass", "setpass-lib.pl");

	foreach $k (keys %$ptulist)
	{
		@line = ();		
		@line[0] = $ptulist->{$k}->{"name"};

		unless($pw->{$ptulist->{$k}->{"name"}}->[0])  #uname
		{
			next;
		}
		
		if($sd->{$ptulist->{$k}->{"name"}}->[0])   #uname
		{
			@line[1] = $sd->{$ptulist->{$k}->{"name"}}->[1];
		}
		else
		{
			
			@line[1] = &foreign_call("setpass", "crt_cryptpasswd", $ptulist->{$k}->{"name"});
		}
		$line[2] = $pw->{$ptulist->{$k}->{"name"}}->[2];    #uid
		$line[3] = $pw->{$ptulist->{$k}->{"name"}}->[3];    #gid
		$line[4] = $pw->{$ptulist->{$k}->{"name"}}->[4];    #full name
		$line[5] = $ptulist->{$k}->{"dir"};    #home dir

		$line[6] = $ptulist->{$k}->{"ulbw"} if($ptulist->{$k}->{"ulbw"});
		$line[7] = $ptulist->{$k}->{"dlbw"} if($ptulist->{$k}->{"dlbw"});
		$line[10] = $ptulist->{$k}->{"maxlink"} if($ptulist->{$k}->{"maxlink"});
		$line[18] = $ptulist->{$k}->{"rw"};

		push(@ftppasswd, join(":", @line) . "\n");
	}

	for($i = 0; $i < @ftppasswd; $i++)
	{
		print "<p>", $ftppasswd[$i];
	}
}

#set_ftpgroup(\%glist, \%gp)
sub set_ftpgroup()
{
	my ($k, $ptglist, @ftpgroup, $i, @line, $gp, @gstruct);

	$ptglist = @_[0];
	$gp = @_[1];

	open(FTPGROUP, $config{'ftpgroup'});
	@ftpgroup = <FTPGROUP>;
	close(FTPGROUP);

#	@group = `getent group`;

#	for($i = 0; $i < @group; $i++)
#	{
#		@line = split(":", $group[$i]);
#		if($line[0])
#		{
#			$gp{$line[0]} = $line[2];
#		}
#	}

	for($i = @ftpgroup - 1; $i >= 0; $i--)
	{
		$ftpgroup[$i] =~ s/#.*$//;
		@line = split(":", $ftpgroup[$i]);
		if($line[0])
		{
			if($ptglist->{$line[0]}->{"name"} && ($ptglist->{$line[0]}->{"type"} eq "group"))
			{
				splice @ftpgroup, $i, 1;
			}
		}
	}

	@gstruct = ();

	foreach $k (sort {$ptglist->{$a}->{"order"} <=> $ptglist->{$b}->{"order"}} keys %$ptglist)
	{
		unless($gp->{$ptglist->{$k}->{"name"}}->[0])
		{
			next;
		}
		@line = ();
		$line[0] = $ptglist->{$k}->{"name"};
		$line[1] = $gp->{$ptglist->{$k}->{"name"}}->[1];   #gid
		$line[2] = $ptglist->{$k}->{"dir"};      #dir
		$line[3] = $ptglist->{$k}->{"ulbw"} if($ptglist->{$k}->{"ulbw"});
		$line[4] = $ptglist->{$k}->{"dlbw"} if($ptglist->{$k}->{"dlbw"});
		$line[7] = $ptglist->{$k}->{"maxlink"} if($ptglist->{$k}->{"maxlink"});
		$line[15] = $ptglist->{$k}->{"rw"};

		push @gstruct, join(":", @line) . "\n";
	}

	splice @ftpgroup, 0, 0, @gstruct;

	for($i = 0; $i < @ftpgroup; $i++)
	{
		print "<p>", $ftpgroup[$i];
	}
}

#sub ftp_ch_trustuser($dir, $in_ulist, \@ftpulist)
sub ftp_ch_trustuser()
{
	my ($ftpulist, $in_ulist, $i, @trust, @line);
	my ($name, $type, $accessmode, $ulbw, $dlbw, $maxlink, $dir, $rec, $k);

	$dir = @_[0];
	$in_ulist = @_[1];
	$ftpulist = @_[2];

	unless(-d $dir)
	{
		return;
	}

	@trust = split(":", $in_ulist);

	for($i = 0; $i < @trust; $i++)
	{
		@line = split(",", $trust[$i]);
		unless($line[1])
		{
			next;
		}
		$type = "user" if($line[0] == 0);
		$type = "group" if($line[0] == 1);
		$type = "user" if($line[0] == 2);
		$type = "group" if($line[0] == 3);
		$name = $line[1];
		$accessmode = $line[2];
		$ulbw = $line[3] * 1024;
		$ulbw = $ulbw * 1024 if($line[4]);  #line[4] unit 0-KB  1-MB
		$ulbw = sprintf "%.0f", $ulbw;
		$dlbw = $line[5] * 1024;
		$dlbw = $dlbw * 1024 if($line[6]);  #line[6] unit 0-KB  1-MB
		$dlbw = sprintf "%.0f", $dlbw;
		$maxlink = $line[7];
		$rec = {};
		$rec->{"type"} = $type;
		$rec->{"name"} = $name;
		$rec->{"ulbw"} = $ulbw if($ulbw);
		$rec->{"dlbw"} = $dlbw if($dlbw);
		$rec->{"maxlink"} = $maxlink;
		$rec->{"dir"} = $dir.'/./';
		$rec->{"rw"} = $accessmode;
		$rec->{"order"} = $i;

		push @$ftpulist , $rec;
	}
}

#fetch_ftp_dir($dir)
sub fetch_ftp_dir()
{
	my ($dir, @shareftp, $i, $k, @line, $find, %udel, %gdel);
	$dir = @_[0];

	open(SHAREFTP, $config{'shareftp'});
	@shareftp = <SHAREFTP>;
	close(SHAREFTP);

	$find = -1;
	for($i = 0; $i < @shareftp; $i++)
	{
		$shareftp[$i] =~ s/#.*$//;
		chomp($shareftp[$i]);
		
		if($shareftp[$i] eq "<$dir>")
		{
			$find = $i;
			next;
		}

		if($find >= 0)
		{
			if($shareftp[$i] =~ /^rouser/)
			{
				$shareftp[$i] =~ s/^rouser\s*=\s*//;
				@line = split(/\s*,\s*/, $shareftp[$i]);
				foreach $k (@line)
				{
					$udel{$k} = $k;
				}
				next;
			}
			
			if($shareftp[$i] =~ /^rwuser/)
			{
				$shareftp[$i] =~ s/^rwuser\s*=\s*//;
				@line = split(/\s*,\s*/, $shareftp[$i]);
				foreach $k (@line)
				{
					$udel{$k} = $k;
				}
				next;
			}

			if($shareftp[$i] =~ /^rogroup/)
			{
				$shareftp[$i] =~ s/^rogroup\s*=\s*//;
				@line = split(/\s*,\s*/, $shareftp[$i]);
				foreach $k (@line)
				{
					$gdel{$k} = $k;
				}
				next;
			}
			
			if($shareftp[$i] =~ /^rwgroup/)
			{
				$shareftp[$i] =~ s/^rwgroup\s*=\s*//;
				@line = split(/\s*,\s*/, $shareftp[$i]);
				foreach $k (@line)
				{
					$gdel{$k} = $k;
				}
				next;
			}
			if($shareftp[$i] eq "<end>")
			{
				splice @shareftp, $find, $i - $find + 1;
				last;
			}
		}

		$shareftp[$i] = $shareftp[$i] . "\n";
	}

	&del_storftppasswd_ulist(\%udel);
	&del_storftpgroup_glist(\%gdel);

	open(SHAREFTP, ">$config{'shareftp'}");
	print SHAREFTP @shareftp;
	close(SHAREFTP);
}

#sub stor_ftppasswd_with_dir(\%ulist, \%sd, \%pw, $dir)
sub stor_ftppasswd_with_dir()
{
	my (@file, @line, $i, $ptulist, $k, $uname, $sd, $pw, %ftpline, $dir);

	$ptulist = @_[0];
	$sd = @_[1];
	$pw = @_[2];
	$dir = @_[3];

	$dir = $dir.'/./';

	open(STORFTPPASSWD, $config{'storftppasswd'});
	@file = <STORFTPPASSWD>;
	close(STORFTPPASSWD);

	for($i = @file - 1; $i >= 0; $i--)
	{

		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(":", $file[$i]);

		if($line[0] eq $ptulist->{$line[0]}->{"name"})
		{
			$ftpline{$line[0]} = [@line];
			splice @file, $i, 1;   #can't delete item for passwd, passwd is needed for user login
			next;
		}
		$file[$i] = $file[$i] . "\n";
	}

	&foreign_require("setpass", "setpass-lib.pl");

	foreach $k (keys %$ptulist)
	{
		$uname = $ptulist->{$k}->{"name"};
		next unless($uname);
		@line = ();
		$line[0] = $uname;           #uname
		if($ftpline{$uname}->[0])
		{
			$line[1] = $ftpline{$uname}->[1];
		}
		elsif($sd->{$uname}->[0])
		{
			$line[1] = $sd->{$uname}->[1];   #passwd
		}
		else
		{
			$line[1] = "!!";   #set passwd to uncheck by ftppasswd just for domain users
		}

		if($pw->{$uname}->[0])
		{
			$line[2] = $pw->{$uname}->[2];   #uid
			$line[3] = $pw->{$uname}->[3];   #gid
			$line[4] = $pw->{$uname}->[4];   #fullname
		}
		else
		{
			$line[2] = $ftpline{$uname}->[2];   #uid
			$line[3] = $ftpline{$uname}->[3];   #gid
			$line[4] = $ftpline{$uname}->[4];   #fullname
		}		

		$line[5] = $ptulist->{$k}->{"dir"};   #dir
		$line[6] = $ptulist->{$k}->{"ulbw"} if ($ptulist->{$k}->{"ulbw"});    #ulbw
		$line[7] = $ptulist->{$k}->{"dlbw"} if ($ptulist->{$k}->{"dlbw"});   #dlbw
		$line[10] = $ptulist->{$k}->{"maxlink"} if ($ptulist->{$k}->{"maxlink"});   #maxlink
		$line[18] = $ptulist->{$k}->{"rw"};   #rw
		
		push @file, join(":", @line) . "\n";
	}

	open(STORFTPPASSWD, ">$config{'storftppasswd'}");
	print STORFTPPASSWD @file;
	close(STORFTPPASSWD);

#	for($i = 0; $i < @file; $i++)
#	{
#		print "<p>", $file[$i];
#	}
}

#stor_ftpgroup_with_dir(\%glist, \%gp, $dir)
sub stor_ftpgroup_with_dir()
{
	my ($i, $k, $ptglist, $gp, @line, @file, $gname, $dir);

	$ptglist = @_[0];
	$gp = @_[1];
	$dir = @_[2];

	$dir = $dir . '/./';
	open(STORFTPGROUP, $config{'storftpgroup'});
	@file = <STORFTPGROUP>;
	close(STORFTPGROUP);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;
		@line = split(":", $file[$i]);

		if($line[0] eq $ptglist->{$line[0]}->{"name"})
		{
			splice @file, $i, 1;
		}
	}

	foreach $k (keys %$ptglist)
	{
		$gname = $ptglist->{$k}->{"name"};
		next unless($gname);
		@line = ();
		$line[0] = $gname;           #gname
		$line[1] = $gp->{$gname}->[2]; #gid
		$line[2] = $ptglist->{$k}->{"dir"};   #dir
		$line[3] = $ptglist->{$k}->{"ulbw"} if ($ptglist->{$k}->{"ulbw"});    #ulbw
		$line[4] = $ptglist->{$k}->{"dlbw"} if ($ptglist->{$k}->{"dlbw"});   #dlbw
		$line[7] = $ptglist->{$k}->{"maxlink"} if ($ptglist->{$k}->{"maxlink"});   #maxlink
		$line[15] = $ptglist->{$k}->{"rw"};   #rw
		
		push @file, join(":", @line) . "\n";
	}

	open(STORFTPGROUP, ">$config{'storftpgroup'}");
	print STORFTPGROUP @file;
	close(STORFTPGROUP);

#	for($i = 0; $i < @file; $i++)
#	{
#		print "<p>", $file[$i];
#	}
}

#get_sd_hash(\%sd)
sub get_sd_hash()
{
	my ($sd, @line, $i, $k, @shadow);
	$sd = @_[0];

	open(SHADOW, $config{'shadow'});
	@shadow = <SHADOW>;
	close(SHADOW);

	for($i = 0; $i < @shadow; $i++)
	{
		$shadow[$i] =~ s/#.*$//;
		chomp($shadow[$i]);
		@line = split(":", $shadow[$i]);
		if($line[0])
		{
			$sd->{$line[0]} = [@line];
		}
	}
}

#get_pw_hash(\%pw)
sub get_pw_hash()
{
	my ($pw, @line, $i, $k, @passwd);

	$pw = @_[0];
	@passwd = `getent passwd`;


	for($i = 0; $i < @passwd; $i++)
	{
		$passwd[$i] =~ s/#.*$//;
		chomp($passwd[$i]);
		@line = split(":", $passwd[$i]);
		if($line[0])
		{
			#[uname, uid, gid, fullname, homedir]
			$pw->{$line[0]} = [@line];
		}
	}
}

#get_grp_hash(\%gp)
#$gp->{$gname}->[(gname, gpw, gid, member)]
sub get_gp_hash()
{
	my ($grp, @line, $i, $k, @group);

	$gp = @_[0];
	@group = `getent group`;

	for($i = 0; $i < @group; $i++)
	{
		$group[$i] =~ s/#.*$//;
		chomp($group[$i]);
		@line = split(":", $group[$i]);
		if($line[0])
		{
			$gp->{$line[0]} = [@line];
		}
	}
}

#del_storftppasswd_ulist(\%udel)
sub del_storftppasswd_ulist()
{
	my ($i, $ptulist, $k, @file, @line);

	$ptudel = @_[0];

	open(STORFTPPASSWD, $config{'storftppasswd'});
	@file = <STORFTPPASSWD>;
	close(STORFTPPASSWD);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;
		@line = split(":", $file[$i]);
		if($ptudel->{$line[0]})
		{
			splice @file, $i, 1;
		}
	}

	open(STORFTPPASSWD, ">$config{'storftppasswd'}");
	print STORFTPPASSWD @file;
	close(STORFTPPASSWD);

#	for($i = 0; $i < @file; $i++)
#	{
#		print "<p>", $file[$i];
#	}
}

#del_storftpgroup_glist(\%gdel)
sub del_storftpgroup_glist()
{
	my ($i, $ptulist, $k, @file, @line);

	$ptgdel = @_[0];

	open(STORFTPGROUP, $config{'storftpgroup'});
	@file = <STORFTPGROUP>;
	close(STORFTPGROUP);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;
		@line = split(":", $file[$i]);
		if($ptgdel->{$line[0]})
		{
			splice @file, $i, 1;
		}
	}

	open(STORFTPGROUP, ">$config{'storftpgroup'}");
	print STORFTPGROUP @file;
	close(STORFTPGROUP);

#	for($i = 0; $i < @file; $i++)
#	{
#		print "<p>", $file[$i];
#	}
}

#reload_puredb(void)
sub reload_puredb()
{
	my $out;

	$out = &ez_system("pure-pw", "mkdb", $config{'ftppdb'});
}

#del_user($uname)
sub del_user()
{
	my ($i, @line, @file, $uname);

	$uname = @_[0];

	open(FTPPASSWD, $config{'ftppasswd'});
	@file = <FTPPASSWD>;	
	close(FTPPASSWD);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;

		@line = split(":", $file[$i]);
		if($line[0] eq $uname)
		{
			splice @file, $i, 1;
		}
	}

	open(FTPPASSWD, ">$config{'ftppasswd'}");
	print FTPPASSWD @file;
	close(FTPPASSWD);

	&reload_puredb();
}

#newuser{"user", "pass", "uid", "gid",  "real", "home"}
#mod_user(\%olduser, \%newuser)
sub mod_user()
{
	my ($ptold, $ptnew, @line, @file, );

	$ptold = @_[0];
	$ptnew = @_[1];

	open(FTPPASSWD, $config{'ftppasswd'});
	@file = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(":", $file[$i]);
		if($line[0] eq $ptold->{"user"})
		{
			splice @file, $i, 1;
			next;
		}

		if($line[0] eq $ptnew->{"user"})
		{
			splice @file, $i, 1;
			next;
		}
		$file[$i] = $file[$i] . "\n";
	}
	@line = &get_useritem_line($ptnew->{"user"});	
	$line[0] = $ptnew->{"user"} if($ptnew->{"user"});
	$line[1] = $ptnew->{"pass"} if ($ptnew->{"pass"});
	$line[2] = $ptnew->{"uid"} if ($ptnew->{"uid"});
	$line[3] = $ptnew->{"gid"} if ($ptnew->{"gid"});
	$line[4] = $ptnew->{"real"} if ($ptnew->{"real"});
	unless($line[5])
	{
		$line[5] = $ptnew->{"home"};
	}
	if($line[18] eq "")
	{
		$line[18] = -1;
	}
	push @file, join(":", @line) . "\n";
	
	open(FTPPASSWD, ">$config{'ftppasswd'}");
	print FTPPASSWD @file;
	close(FTPPASSWD);

	&reload_puredb();	
}

#disable_user($uname)
sub disable_user()
{
	my ($uname, @line, @file);
	
	$uname = @_[0];

	open(FTPPASSWD, $config{'ftppasswd'});
	@file = <FTPPASSWD>;	
	close(FTPPASSWD);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		@line = split(":", $file[$i]);
		if($line[0] eq $uname)
		{
			$line[1] =~ s/^!+//;
			$line[1] = "!!" . $line[1];
			$file[$i] = join(":", @line);
			last;
		}
	}

	open(FTPPASSWD, ">$config{'ftppasswd'}");
	print FTPPASSWD @file;
	close(FTPPASSWD);

	&reload_puredb();	
}

#enable_user($uname)
sub enable_user()
{
	my ($uname, @line, @file);
	
	$uname = @_[0];

	open(FTPPASSWD, $config{'ftppasswd'});
	@file = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		@line = split(":", $file[$i]);
		if($line[0] eq $uname)
		{
			$line[1] =~ s/^!+//;
			$line[1] = "!!" unless($line[1]);
			$file[$i] = join(":", @line);
			last;
		}
	}

	open(FTPPASSWD, ">$config{'ftppasswd'}");
	print FTPPASSWD @file;
	close(FTPPASSWD);

	&reload_puredb();	
}

#add_user(\%user)
#user{"user", "pass", "home", "uid", "gid", "real"}
sub add_user()
{
	my ($ptnew, @line, @file);

	$ptnew = @_[0];

	open(FTPPASSWD, $config{'ftppasswd'});
	@file = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;
		@line = split(":", $file[$i]);
		if($line[0] eq $ptnew->{"user"})
		{

			splice @file, $i, 1;
		}
	}

	@line = &get_useritem_line($ptnew->{"user"});
	$line[0] = $ptnew->{"user"};
	$line[1] = $ptnew->{"pass"};
	$line[2] = $ptnew->{"uid"};
	$line[3] = $ptnew->{"gid"};
	$line[4] = $ptnew->{"real"};
	unless($line[5])
	{
		$line[5] = $ptnew->{"home"};
	}
	if($line[18] eq "")
	{
		$line[18] = -1;
	}

	push @file, join(":", @line) . "\n";

	open(FTPPASSWD, ">$config{'ftppasswd'}");
	print FTPPASSWD @file;
	close(FTPPASSWD);

	&reload_puredb();	
}

#mod_group(\%oldgroup, \%newgroup)
sub mod_group()
{
	my ($i, @line, @file, $ptold, $ptnew, %gpsort);

	$ptold = @_[0];
	$ptnew = @_[1];

	open(FTPGROUP, $config{'ftpgroup'});
	@file = <FTPGROUP>;	
	close(FTPGROUP);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(":", $file[$i]);
		if($line[0] eq $ptold->{"group"})
		{
			splice @file, $i, 1;
			next;			
		}
		if($line[0] eq $ptnew->{"group"})
		{
			splice @file, $i, 1;
			next;
		}
		$file[$i] = $file[$i] . "\n";
	}

	@line = &get_groupitem_line($ptnew->{"group"});
	if($line[0])
	{
		$line[0] = $ptnew->{"group"};
		$line[1] = $ptnew->{"gid"};
		if($line[15] eq "")
		{
			$line[15] = -1;
		}
		push @file, join(":", @line) . "\n";
	}

	&get_ftpgroup_sorthash(\%gpsort);
	@file = sort {$gpsort{&fst_element_by_linestr($a, ":")} <=> $gpsort{&fst_element_by_linestr($b, ":")}} @file;

	open(FTPGROUP, ">$config{'ftpgroup'}");
	print FTPGROUP @file;
	close(FTPGROUP);
}

#del_group($gname)
sub del_group()
{
	my ($i, @line, @file, $gname);

	$gname = @_[0];

	open(FTPGROUP, $config{'ftpgroup'});
	@file = <FTPGROUP>;
	close(FTPGROUP);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;

		@line = split(":", $file[$i]);
		if($line[0] eq $gname)
		{
			splice @file, $i, 1;
		}
	}

	open(FTPGROUP, ">$config{'ftpgroup'}");
	print FTPGROUP @file;
	close(FTPGROUP);
}

sub add_group()
{
	my ($i, @line, @file, $ptnew, %gpsort);

	$ptnew = @_[0];

	open(FTPGROUP, $config{'ftpgroup'});
	@file = <FTPGROUP>;	
	close(FTPGROUP);

	for($i = @file - 1; $i >= 0; $i--)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(":", $file[$i]);

		if($line[0] eq $ptnew->{"group"})
		{
			splice @file, $i, 1;
			next;
		}
		$file[$i] = $file[$i] . "\n";
	}

	@line = &get_groupitem_line($ptnew->{"group"});
	if($line[0])
	{
		$line[0] = $ptnew->{"group"};
		$line[1] = $ptnew->{"gid"};

		if($line[15] eq "")
		{
			$line[15] = -1;
		}
		push @file, join(":", @line) . "\n";
	}

	&get_ftpgroup_sorthash(\%gpsort);
	@file = sort {$gpsort{&fst_element_by_linestr($a, ":")} <=> $gpsort{&fst_element_by_linestr($b, ":")}} @file;

	open(FTPGROUP, ">$config{'ftpgroup'}");
	print FTPGROUP @file;
	close(FTPGROUP);
}

#get_ftpgroup_sorthash(\%gpsort)
sub get_ftpgroup_sorthash()
{
	my ($ptsort, @line, @file, $k, $i);

	$ptsort = @_[0];

	open(FTPPASSWD, $config{'storftpgroup'});
	@file = <FTPPASSWD>;
	close(FTPPASSWD);
	
	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(":", $file[$i]);
		if($line[0])
		{
			$ptsort->{$line[0]} = $i;
		}
	}
}

#get_useritem_line($uname)
sub get_useritem_line()
{
	my ($uname, $i, @line, @file);
	
	$uname = @_[0];

	open(FTPPASSWD, $config{'storftppasswd'});
	@file = <FTPPASSWD>;
	close(FTPPASSWD);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(":", $file[$i]);
		if($line[0] eq $uname)
		{
			return @line;
		}
	}
	return ();
}

#get_groupitem_line($gname)
sub get_groupitem_line()
{
	my ($gname, $i, @line, @file);
	
	$gname = @_[0];

	open(STORFTPGROUP, $config{'storftpgroup'});
	@file = <STORFTPGROUP>;
	close(STORFTPGROUP);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(":", $file[$i]);
		if($line[0] eq $gname)
		{
			return @line;
		}
	}
	return ();
}

#set_global_config(\%gbconfig)
sub set_global_config()
{
	my (@line, $i, $lref, $ptgb, $str);

	$ptgb = @_[0];

 	$lref = &read_file_lines($config{"ftpconf"});
	for($i = @$lref - 1; $i >= 0; $i--)
	{
		$str = $lref->[$i];
		$str =~ s/^\s+//;
		@line = split(/\s+/, $str);
		if($ptgb->{$line[0]} ne "")
		{
			splice @$lref, $i, 1;
			next;
		}
	}

	foreach $k (keys %$ptgb)
	{
		if($ptgb->{$k})
		{
			push @$lref, $k . "        " . $ptgb->{$k};
		}
	}
	
	&flush_file_lines();
}

#get_global_config(\%gbconfig)
sub get_global_config()
{
	my (@line, $i, $ptgb, @file);
	
	$ptgb = @_[0];

	open(PURECONF, $config{'ftpconf'});
	@file = <PURECONF>;
	close(PURECONF);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		if($file[$i] =~ /^(\s*)MaxClientsPerIP/)
		{
			$file[$i] =~ s/^\s+//;
			@line = split(/\s+/, $file[$i]);
			$ptgb->{"MaxClientsPerIP"} = $line[1];
			next;
		}

		if($file[$i] =~ /^(\s*)MaxClientsNumber/)
		{
			$file[$i] =~ s/^\s+//;
			@line = split(/\s+/, $file[$i]);
			$ptgb->{"MaxClientsNumber"} = $line[1];
			next;
		}

		if($file[$i] =~ /^(\s*)AnonymousBandwidth/)
		{
			$file[$i] =~ s/^\s+//;
			@line = split(/\s+/, $file[$i]);
			$ptgb->{"AnonymousBandwidth"} = $line[1];
			next;
		}

		if($file[$i] =~ /^(\s*)PerUserLimits/)
		{
			$file[$i] =~ s/^\s+//;
			@line = split(/\s+/, $file[$i]);
			$ptgb->{"PerUserLimits"} = $line[1];
			next;
		}
                if($file[$i] =~ /^(\s*)Bind/)
                {
                        $file[$i] =~ s/^\s+//;
                        @line = split(/\s+/, $file[$i]);
                        $ptgb->{"Bind"} = $line[1];
                        next;
                }
	}
}

#get_anonymous_home(void)
sub get_anonymous_home()
{
	my ($i, @line, @file);

	open(PASSWD, $config{"passwd"});
	@file = <PASSWD>;
	close(PASSWD);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);

		@line = split(":", $file[$i]);

		if($line[0] eq $config{"anonyuname"})
		{
			return $line[5];			
		}
	}
	return "";
}

#set_anonymous_home($home)
sub set_anonymous_home()
{
	my ($i, @file, @line, $home);

	$home = @_[0];

	open(PASSWD, $config{"passwd"});
	@file = <PASSWD>;
	close(PASSWD);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);

		@line = split(":", $file[$i]);

		if($line[0] eq $config{"anonyuname"})
		{
			$line[5] = $home;
			$file[$i] = join(":", @line) . "\n";
			last;
		}
		$file[$i] = $file[$i] . "\n";
	}

	open(PASSWD, ">$config{'passwd'}");
	print PASSWD @file;
	close(PASSWD);
}

#get_shareftp_u2d_g2d(\%u2d, \%g2d)
sub get_shareftp_u2d_g2d()
{
	my ($ptu2d, $ptg2d, $i, @line, @file, $dirname);

	$ptu2d = @_[0];
	$ptg2d = @_[1];

	open(SHAREFTP, $config{'shareftp'});
	@file = <SHAREFTP>;
	close(SHAREFTP);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);

		if($file[$i] =~ /^<\/(.*)>$/)
		{
			$dirname = "/".$1."/./";
			next;
		}

		if($dirname && $file[$i] =~ /^rouser/)
		{
			$file[$i] =~ s/^rouser\s*=\s*//;

			@line = split(/\s*,\s*/, $file[$i]);

			foreach $k (@line)
			{
				$ptu2d->{$k} = $dirname unless($ptu2d->{$k});
			}
			next;
		}

		if($dirname && $file[$i] =~ /^rwuser/)
		{
			$file[$i] =~ s/^rwuser\s*=\s*//;
			@line = split(/\s*,\s*/, $file[$i]);

			foreach $k (@line)
			{
				$ptu2d->{$k} = $dirname unless($ptu2d->{$k});
			}
			next;
		}

		if($dirname && $file[$i] =~ /^rogroup/)
		{
			$file[$i] =~ s/^rogroup\s*=\s*//;
			@line = split(/\s*,\s*/, $file[$i]);

			foreach $k (@line)
			{
				$ptg2d->{$k} = $dirname unless($ptg2d->{$k});

			}
			next;
		}

		if($dirname && $file[$i] =~ /^rwgroup/)
		{
			$file[$i] =~ s/^rwgroup\s*=\s*//;
			@line = split(/\s*,\s*/, $file[$i]);

			foreach $k (@line)
			{
				$ptg2d->{$k} = $dirname unless($ptg2d->{$k});

			}
			next;
		}

		if($file[$i] eq "<end>")
		{
			$dirname = "";
			next;
		}
	}	
}

#@uglist is array of hash, each element is hash \%rec,
#\%rec{"name", "type", "rw", "dir", "ulbw", "dlbw", "maxlink", "order"}
sub check_uglist(\@uglist)
{
	my ($i, $ptlist, @line, @file, $k, $rec, %u2d, %g2d, %sharetab, $dirname, @umsg, @gmsg, $rt_msg);

	$ptlist = @_[0];

	#%u2d is in shareftp user to dir hash
	#%g2d is in shareftp group to dir hash

	&get_shareftp_u2d_g2d(\%u2d, \%g2d);

	&foreign_require("shareman", "shareman-lib.pl");
	&foreign_call("shareman", "read_sharetab", \%sharetab, 2);   #sharepath is key

	for($i = 0; $i < @$ptlist; $i++)
	{
		$rec = $ptlist->[$i];

		if(($rec->{"type"} eq "user") && $u2d{$rec->{"name"}})
		{
			if($rec->{"dir"} ne $u2d{$rec->{"name"}})
			{
				$dirname = $rec->{"dir"};
				$dirname =~ s/\/\.\/$//;  # delete '/./' in last dirname
				push @umsg, &text("msg_user_conflict", $rec->{"name"}, $sharetab{$dirname}->{"sharename"});
				next;
			}
		}


		if(($rec->{"type"} eq "group") && $g2d{$rec->{"name"}})
		{

			if($rec->{"dir"} ne $g2d{$rec->{"name"}})
			{
				$dirname = $rec->{"dir"};

				$dirname =~ s/\/\.\/$//;  # delete '/./' in last dirname

				push @gmsg, &text("msg_group_conflict", $rec->{"name"}, $sharetab{$dirname}->{"sharename"});
				next;
			}
		}
	}

	splice @gmsg, 0, 0, @umsg if(@umsg > 0);

	$rt_msg = join('\n', @gmsg);
	return $rt_msg;
}

1;
